package com.wayne.testapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.wayne.testapp.R;
import com.wayne.testapp.adapter.RecycleAdapter;
import com.wayne.testapp.ui.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wayne on 2016/06/27.
 */
public class TestRecycleActivity extends BaseActivity {

    private SwipeRefreshLayout srl_refresh_widget;
    private EmptyRecyclerView rv_test;
    private RecycleAdapter adapter;
    private LinearLayoutManager mLayoutManager;
    private List<String> list;


    private Handler handler = new Handler();
    int addCount = 8888;
    int lastVisibleItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_test_recycle);
        initView();
    }

    private void initView() {
        rv_test = (EmptyRecyclerView) findViewById(R.id.rv_test);
        srl_refresh_widget = (SwipeRefreshLayout) findViewById(R.id.srl_refresh_widget);

        adapter = new RecycleAdapter();
        list = getData();
        adapter.setList(list);

        ImageView iv = new ImageView(this);
        iv.setImageResource(R.mipmap.code_ios);
        rv_test.setEmptyView(iv);



        srl_refresh_widget.setColorSchemeColors(Color.RED);
        srl_refresh_widget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (list != null) {
                    list.remove(0);
                    adapter.notifyDataSetChanged();
                    srl_refresh_widget.setRefreshing(false);
                }
            }
        });
        rv_test.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //RecyclerView没有拖动而且已经到达了最后一个item，执行自动加载
                System.out.println("lastVisibleItem--"+lastVisibleItem);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem == adapter.getItemCount()) {

                    for (int i = 0; i < 3; i++) {
                        String item = "测试数据" + addCount;
                        addCount += 1;
                        list.add(item);
                    }

                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();

                        }
                    },1500);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
            }
        });
        //创建布局管理器
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_test.setLayoutManager(mLayoutManager);

        rv_test.setAdapter(adapter);

        System.out.println("size---"+adapter.getItemCount());
    }

    private List<String> getData() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            list.add("测试数据" + i);
        }
        return new ArrayList<>();
    }
}
